package com.marketingexpert.controller.personmgnt;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.User;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.WebUtils;

public class GenerateWeiboAccount implements Controller {

	private UserService userService;
	
	private SnsServiceFactory snsServiceFactory;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String userId = (String)request.getParameter("userId");
		com.marketingexpert.domain.User myUser = userService.queryUserByUId(userId);
		
		SnsService weboauth = WebUtils.getSnsService(myUser, getSnsServiceFactory());
		
		com.marketingexpert.service.snsmgnt.User user = weboauth.getUserDetails(myUser.getAccessToken(), myUser.getSecret(), userId);
		
		Map<String,String> result = new HashMap<String,String>();
		response.setContentType("application/x-json;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		PrintWriter out = response.getWriter(); 
		result.put("userId", String.valueOf(user.getId()));
		result.put("weiboNickUser", user.getName());
		
		JSONObject jsonList = JSONObject.fromObject(result); 
		
		out.print(jsonList);
		
		return null;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}

}
